package com.kotlin.usermodel.service.impl

import com.kotlin.baselibrary.data.protocol.BaseResponse
import com.kotlin.baselibrary.rx.BaseException
import com.kotlin.usermodel.data.repository.UserRepository
import com.kotlin.usermodel.service.UserService
import io.reactivex.Observable
import io.reactivex.functions.Function
import javax.inject.Inject

/**
 * @author       zhipe
 * @createTime    2020/10/12
 * @description
 */
class UserServiceImpl2 @Inject constructor() : UserService {
    @Inject
    lateinit var repository: UserRepository

    override fun register(
        phoneNumber: String,
        verifyCode: String,
        pwd: String
    ): Observable<Boolean> {
        return repository.register(phoneNumber, pwd, verifyCode)
            .flatMap<Boolean>(object : Function<BaseResponse<Boolean>, Observable<Boolean>> {
                override fun apply(t: BaseResponse<Boolean>): Observable<Boolean> {
                    if (t.status != 0) {
                        return Observable.error(BaseException(t.status, t.message))
                    }
                    return Observable.just(false)
                }
            })
    }
}